#region Using Directives
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Xml;

#endregion

namespace Secure.Client
{
    public static class XmlSecureClientConfigurator
    {
        public static bool Configure(ISecureClient client)
        {
            XmlNode rootNode = (XmlNode)ConfigurationManager.GetSection("secureClient");

            foreach (XmlNode roleNode in rootNode.ChildNodes)
            {
                if (roleNode.Name == "role" && roleNode.Attributes["name"] != null)
                {
                    string roleName = roleNode.Attributes["name"].Value;
                    client.CreateRole(roleName);

                    foreach (XmlNode userNode in roleNode.ChildNodes)
                    {
                        if (userNode.Name == "user" && userNode.Attributes["login"] != null)
                        {
                            string loginId = userNode.Attributes["login"].Value;
                            List<string> additionalParams = new List<string>();

                            foreach (XmlNode paramNode in userNode.ChildNodes)
                            {
                                if (paramNode.Name == "param" && paramNode.Attributes["value"] != null)
                                {
                                    additionalParams.Add(paramNode.Attributes["value"].Value);
                                }
                            }

                            client.AddUserToRole(loginId, roleName, additionalParams.ToArray());
                        }
                    }
                }
            }

            return true;
        }
    }
}